import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.concurrent.ExecutionException;

import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.SwingWorker;

import paskal.abstractplayer.AbstractPlayer;
import paskal.abstractplayer.Playlist;
import paskal.ui.components.Button;
import paskal.ui.components.Label;
import paskal.ui.components.List;
import paskal.ui.components.Panel;
import paskal.ui.components.ScrollPane;
import paskal.ui.components.TextArea;
import paskal.ui.components.TextField;

public class PlaylistPanel extends Panel {
	private static final long serialVersionUID = 960992451002353043L;

	private AbstractPlayer player;
	private List<Object> trackList = null;
	private Label waitingLabel;
	private TextArea lyrics;
	private boolean isLyricsVisible = false;
	private ScrollPane scroll;
	private Button showLyrics;

	public PlaylistPanel(AbstractPlayer player) {
		this.player = player;
		setLayout(null);
		stylize("playlistPanel");
		init();
	}

	private void init() {
		setDoubleBuffered(true);
		trackList = new List<Object>();
		trackList.stylize("trackList");
		trackList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if (e.getClickCount() > 1) {
					trackList.setActiveIndex(trackList.getSelectedIndex());
					player.play(trackList.getSelectedIndex());
				}
			}
		});

		scroll = new ScrollPane(trackList,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setBounds(trackList.getBounds());
		add(scroll);

		final TextField search = new TextField();
		search.stylize("searchField");
		search.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					@Override
					public void run() {
						setWaitingVisible(true);
						Playlist playlist = Grabber.search(search.getText());
						trackList.removeAll();
						trackList.setListData(playlist.getPlaylist().toArray());
						trackList.setSelectedIndex(playlist.getCursor());
						player.setPlaylist(playlist);
						setWaitingVisible(false);
					}
				}).start();
			}
		});
		add(search);

		Label searchLabel = new Label();
		searchLabel.stylize("searchLabel");
		search.add(searchLabel);

		Button fileOpen = new Button();
		fileOpen.stylize("fileDirOpen");
		fileOpen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser(System
						.getProperty("user.home"));
				chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

				if (chooser.showOpenDialog(PlaylistPanel.this) == JFileChooser.APPROVE_OPTION) {
					File tempFile = chooser.getSelectedFile();
					if (tempFile != null) {
						setWaitingVisible(true);
						String temp = tempFile.getPath();
						Playlist playlist = null;
						if (tempFile.isFile()) {
							temp = tempFile.getParent();
							playlist = Grabber.openFolder(temp, tempFile);
						} else {
							playlist = Grabber.openFolder(temp, null);
						}
						trackList.removeAll();
						trackList.setListData(playlist.getPlaylist().toArray());
						trackList.setSelectedIndex(playlist.getCursor());
						player.setPlaylist(playlist);
						search.setText(temp);
						setWaitingVisible(false);
					}
				}
				Main.getInstance().showItself();
			}
		});
		search.add(fileOpen);

		waitingLabel = new Label();
		waitingLabel.stylize("waitingLabel");
		waitingLabel.setVisible(false);
		add(waitingLabel);

		lyrics = new TextArea();
		lyrics.stylize("lyrics");

		showLyrics = new Button();
		showLyrics.stylize("showLyrics");
		showLyrics.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!isLyricsVisible) {
					setWaitingVisible(true);
					SwingWorker<String, Void> sw = new SwingWorker<String, Void>() {
						@Override
						protected String doInBackground() throws Exception {
							return Grabber.getLyrics(player.getPlaylist().get()
									.getLyricsId());
						}
					};
					sw.execute();
					try {
						lyrics.setText(sw.get());
						lyrics.setCaretPosition(0);
						scroll.setViewportView(lyrics);
						isLyricsVisible = true;
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
					setWaitingVisible(false);
				} else {
					scroll.setViewportView(trackList);
					isLyricsVisible = false;
				}
			}
		});
		showLyrics.setVisible(false);
		search.add(showLyrics);
	}

	public void setWaitingVisible(boolean value) {
		waitingLabel.setVisible(value);
		scroll.setVisible(!value);
	}

	public void updateShowLirycsButtonVisible() {
		if (player.getPlaylist().get().getLyricsId() != null) {
			showLyrics.setVisible(true);
		} else {
			showLyrics.setVisible(false);
		}
	}

	public void hideLyrics() {
		scroll.setViewportView(trackList);
		isLyricsVisible = false;
	}

	public void updateActiveTrackIndex() {
		trackList.setActiveIndex(player.getPlaylist().getCursor());
	}
}